package com.unitd.modules.sso.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.unitd.frame.sso.annotation.Action;
import com.unitd.frame.sso.annotation.Login;
import com.unitd.frame.sso.annotation.Permission;
import com.unitd.frame.comm.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @desc 验证码服务,用于生成和验证验证码
 * @filename CaptchaController.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/4
 */
@Controller
@RequestMapping("/captcha")
public class CaptchaController {
	private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
	@Autowired
	private Producer captchaProducer = null;

	/**
	 * @desc 生成最新的验证码及图片
	 * @param request http请求对象
	 * @param response http响应对象
	 * @throws Exception
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("image")
	public void createImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
		System.out.println("sessionCode:" + code);
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");
		String capText = captchaProducer.createText();
		session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
		log.info("******************当前  验证码是: " + capText + "******************");
		BufferedImage bi = captchaProducer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bi, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
	}

	/**
	 * @desc 验证用户输入的验证码是否正确
	 * @param validateCode 前台用户输入的验证码
	 * @param request http请求对象
	 * @return 验证码输入正确与否
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("verify")
	@ResponseBody
	public boolean verifyCode(@RequestParam("validateCode") String validateCode, HttpServletRequest request){
		String kaptchaExpected = (String)request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		if(StringUtils.isNotBlank(validateCode) && StringUtils.equalsIgnoreCase(kaptchaExpected, validateCode) ){
			return true;
		}
		return false;
	}
}